﻿/**************************************************************************
创建时间:	2020/6/28
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using ZhCun.DbCore.Cores;
using ZhCun.DbCore.Entitys;
using ZhCun.Utils;
using ZhCun.Utils.Helpers;

namespace ZhCun.WebUtils.DB
{
    public class BLLBase : IApiResultExtend
    {
        public BLLBase()
        {
            DB = new DBMain();
        }

        public virtual DBMain DB { get; }

        public void SetLoginSession(ILoginSession session)
        {
            DB.LoginSession = session;
        }

        /// <summary>
        /// 当前登陆用户Id
        /// </summary>
        public string CurrUserId
        {
            get
            {
                return DB.LoginSession?.CurrUserId;
            }
        }

        /// <summary>
        /// 当前登陆用户Id
        /// </summary>
        public string CurrRoleId
        {
            get
            {
                return DB.LoginSession?.CurrRoleId;
            }
        }

        /// <summary>
        /// 创建一个自定义Guid
        /// </summary>
        public string NewId()
        {
            return GuidHelper.NewId();
        }

        public DateTime GetNowTime()
        {
            return DateTime.Now;
        }

        public virtual void WriteLog(string log)
        {
            //TODO: 写入数据库日志
        }

        public virtual List<TEntity> GetPagerList<TEntity>(int pageNo, int pageSize, out int total, Action<QueryCondition<TEntity>> QueryHandle)
            where TEntity : EntityBase, new()
        {
            var query = DB.CreateQuery<TEntity>();
            query.PageNo = pageNo;
            query.PageSize = pageSize;
            QueryHandle(query);
            var rList = DB.Query<TEntity>(query).ToList(true);
            total = query.Total;
            return rList;
        }

    }
}